package br.com.pesca.converter;

import br.com.pesca.dao.PeixeDAO;
import br.com.pesca.entity.Peixe;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

public class PeixeConverter implements Converter {

    private final PeixeDAO peixeDAO = new PeixeDAO();

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        if (string.equals("vazio")) {
            return new Peixe();
        } else {
            peixeDAO.startOperation();
            Object obj = peixeDAO.find(Peixe.class, Long.parseLong(string));
            peixeDAO.stopOperation(true);
            return obj;
        }
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        if (o.equals("vazio")) {
            return "vazio";
        } else {
            return String.valueOf(((Peixe) o).getId());
        }
    }

}
